#include "mtimer.h"
#include "kernelprivate.h"

MTimer::MTimer(MObject *parent) :
	MObject(*new MTimerPrivate(this), parent)
{
	d_func()->id = -1;
	d_func()->time = 1000;
}

MTimer::~MTimer()
{

}

int MTimer::time()
{
	return d_func()->time;
}

bool MTimer::isStart()
{
	return d_func()->id != -1;
}

void MTimer::setTime(int msecs)
{
	d_func()->time = msecs;
	if( d_func()->id != -1 )
		setTimerTimeout(d_func()->id, msecs);
}

void MTimer::start(int msecs)
{
	d_func()->time = msecs;
	d_func()->id = startTimer(msecs);
}

void MTimer::start()
{
	d_func()->id = startTimer(d_func()->time);
}

void MTimer::stop()
{
	killTimer(d_func()->id);
	d_func()->id = -1;
}

void MTimer::timerEvent(MTimerEvent *event)
{
	if( event->ID() == d_func()->id )
		emit timeout();
}
